package com.sidom.movieIndexer.service.clean.strategy;

import java.util.Set;

import javax.annotation.PostConstruct;

import static org.apache.commons.lang.Validate.notNull;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * Strategy that will remove remove words from the {@link Movie#getName()} specified in {@link #wordsToRemove} {@link Set}.
 * @author Siebe
 */
@Service
public class RemoveWordStrategy implements CleanFileNameStrategy{
	/**
	 * Logger for the {@link RemoveWordStrategy}.
	 */
	private Log log = LogFactory.getLog(RemoveWordStrategy.class);
	
	/**
	 * Words that need to be removed from the file name. e.g. CD1 removes CD1 from Transporter CD1.
	 * Property injected by Spring. 
	 * See movieIndexer.properties.
	 */
	@Value("${removeWordFilter}")
	private String removeWordFilter;
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String cleanFileName(final String fileName, final Set<String> wordsToRemove) {
		notNull(fileName, "fileName can not be null");
		Set<String> removeWords = wordsToRemove;
		if (removeWords==null) {
			removeWords = StringUtils.commaDelimitedListToSet(removeWordFilter);
		}
		String cleanedUpFileName = fileName;
		for (String filter : removeWords) {
			cleanedUpFileName = cleanedUpFileName.replace(filter, "");
		}
		return cleanedUpFileName.trim();
	}

	/**
	 * Check the attributes for null after the properties have been set.
	 */
	@PostConstruct
	public void afterPropertiesSet() {
		Assert.notNull(removeWordFilter);
		log.debug("removeWordFilter used: " + removeWordFilter);
	}
}
